package com.acme.bankapp.domain.bank;

import com.acme.bankapp.exceptions.NotEnoughFundsException;
import com.acme.bankapp.exceptions.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount {
	private static final long serialVersionUID = 8749952575491941027L;

	static final String MES_NEGATIVE_OVERLAP = "Overlap must be positive or zero value";
	
	private double overdraft;
	
	public CheckingAccount(double balance, double overdraft) throws IllegalArgumentException {
		super(balance);
		if (overdraft < 0)
			throw new IllegalArgumentException(MES_NEGATIVE_OVERLAP);
		this.overdraft = overdraft;
	}

	@Override
	public void withDraw(double x) throws IllegalArgumentException, NotEnoughFundsException {
		assert (x > 0);
		assert (balance + overdraft >= 0);
		
		if (x <= 0)
			throw new IllegalArgumentException(MES_NEGATIVE_DEPOSIT);
		else if (balance + overdraft < x) 
			throw new OverDraftLimitExceededException(MES_TOO_SMALL_BALANCE, this);
		
		balance -= x;
		
		assert (balance + overdraft >= 0);
	}

	@Override
	public double maximumAmountToWithdraw() {
		return balance + overdraft;
	}
	
	public double getOverdraft() {
		return overdraft;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(overdraft);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CheckingAccount other = (CheckingAccount) obj;
		if (Double.doubleToLongBits(balance) != 
			Double.doubleToLongBits(other.balance))
			return false;
		if (Double.doubleToLongBits(overdraft) != 
			Double.doubleToLongBits(other.overdraft))
			return false;
		return true;
	}

	public String toString() {
		return "Account:" + "balance = " + decimalValue() + "; overdraft = " + overdraft;
	}	
}
